package com.hrb.douban.adapter;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hrb.douban.CustomApplication;
import com.hrb.douban.R;
import com.hrb.douban.activtity.SearchResultActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HotWordAdapter extends RecyclerView.Adapter<HotWordAdapter.HotwordHolder> {

    private String[] hotWords;

    @NonNull
    @Override
    public HotwordHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_hotword, viewGroup, false);
        HotWordAdapter.HotwordHolder vholder = new HotWordAdapter.HotwordHolder(view);
       /* GridLayoutManager.LayoutParams linearParams =(GridLayoutManager.LayoutParams) vholder.hotwordItem.getLayoutParams();
        linearParams.width=LinearLayout.LayoutParams.WRAP_CONTENT;*/
        LinearLayout.LayoutParams ll=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        GridLayout.LayoutParams gl=new GridLayout.LayoutParams(ll);
        gl.bottomMargin=6;
        gl.rightMargin=12;
        gl.width=LinearLayout.LayoutParams.WRAP_CONTENT;
        vholder.hotwordItem.setLayoutParams(gl);
        return vholder;
    }

    @Override
    public void onBindViewHolder(@NonNull HotwordHolder hotwordHolder, int i) {
        hotwordHolder.hotwordItem.setText(hotWords[i]);
        hotwordHolder.hotwordItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String category= ((TextView)v).getText().toString();
                Intent intent=new Intent(CustomApplication.getContext(),SearchResultActivity.class);
                intent.putExtra("category",category);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                CustomApplication.getContext().startActivity(intent);
            }
        });
    }


    @Override
    public int getItemCount() {
        return hotWords.length;
    }

    public HotWordAdapter(String[] hotWords) {
        this.hotWords = hotWords;
    }

    public class HotwordHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        @BindView(R.id.hotword_item)
        TextView hotwordItem;
        public HotwordHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @Override
        public void onClick(View v) {

        }
    }
}
